/*
  * Mirante Tecnologia
  * Copyright 2010, Mirante Informatica LTDA, 
  * and individual contributors as indicated by the @authors tag
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
package com.miranteinfo.seam.hibernate;

import java.util.Map;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.event.DeleteEvent;
import org.hibernate.event.DeleteEventListener;
import org.hibernate.event.MergeEvent;
import org.hibernate.event.MergeEventListener;
import org.hibernate.event.PersistEvent;
import org.hibernate.event.PersistEventListener;
import org.hibernate.event.ReplicateEvent;
import org.hibernate.event.ReplicateEventListener;
import org.hibernate.event.SaveOrUpdateEvent;
import org.hibernate.event.SaveOrUpdateEventListener;

import com.miranteinfo.seam.hibernate.util.HibernateProxyExtractor;

/**
 * Listener responsavel por sujar a entidade perante o mirante-seam
 * 
 * @author lucas lins
 *
 */
@SuppressWarnings("rawtypes")
public class DirtyListener implements 	SaveOrUpdateEventListener, 
										DeleteEventListener, 
										PersistEventListener, 
										ReplicateEventListener, 
										MergeEventListener{

	private static final long serialVersionUID = 1L;

	// SAVE, SAVE_OR_UPDATE AND UPDATE
	
	public void onSaveOrUpdate(SaveOrUpdateEvent event) throws HibernateException {
		makeDirty(event.getObject());
	}
	
	// DELETE
	
	public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException {
		makeDirty(event.getObject());
	}
	
	public void onDelete(DeleteEvent event) throws HibernateException {
		makeDirty(event.getObject());
	}
	
	// PERSIST
	
	public void onPersist(PersistEvent event) throws HibernateException {
		makeDirty(event.getObject());
	}

	public void onPersist(PersistEvent event, Map createdAlready) throws HibernateException {
		makeDirty(event.getObject());
	}
	
	// REPLICATE
	
	public void onReplicate(ReplicateEvent event) throws HibernateException {
		makeDirty(event.getObject());
	}
	
	// MERGE
	
	public void onMerge(MergeEvent event) throws HibernateException {
		makeDirty(event.getResult());
	}

	public void onMerge(MergeEvent event, Map copiedAlready) throws HibernateException {
		makeDirty(event.getResult());
	}
	
	/**
	 * Makes entity dirty.
	 * @param entity
	 */
	private void makeDirty(Object entity){
		entity = HibernateProxyExtractor.extractEntity(entity);
		if(entity != null && entity instanceof BaseEntity){
			((BaseEntity)entity).makeDirty();
		}
	}
	
}
